import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Solitudefire
 * Date: 2022-08-15
 * Time: 14:47
 */
class MyArray1 {
    public Object[] objects = new Object[10];

    public void set(int pos, Object val) {
        objects[pos] = val;
    }

    public Object get(int pos) {
        return objects[pos];
    }
}

/**
 * @param <T> 此时代表当前类是一个泛型类，T:当作一个占位符
 */
class MyArray2<T extends Number> {
    //public T[] objects = new T[10];//ERROR 1.不能实例化泛型类型的数组
    public T[] objects = (T[]) new Object[10];//也不可以
    //public Object[] objects = (Object[]) new Object[10];

    public void set(int pos, T val) {
        objects[pos] = val;
    }

    public T get(int pos) {
        return objects[pos];
    }

    //Object
    public T[] getArray() {
        return objects;
    }
}

//写一个泛型类，求出一个数组的最大值
class Alg<T extends Comparable<T>> {
    public T findMaX(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            //这里为什么会报错？？？
            //if (max < array[i]) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2 {
    public static <T extends Comparable<T>> T findMaX(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            //这里为什么会报错？？？
            //if (max < array[i]) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg3 {
    public static <T> void print1(ArrayList<T> list) {
        for (T x : list) {
            System.out.println(x);
        }
    }

    public static void print2(ArrayList<?> list) {
        for (Object x : list) {
            System.out.println(x);
        }
    }
}

class Person {

}

class Student extends Person {

}

public class TestDemo {
    public static void main(String[] args) {
        ArrayList<? super Person> arrayList1 = new ArrayList<>();
        //ArrayList<? super Person> arrayList2 = new ArrayList<Student>();
        arrayList1.add(new Person());
        arrayList1.add(new Student());//添加的元素 是Person或者Person的子类
        //Person person = arrayList1.get(0);//
        Object o = arrayList1.get(0);
    }

    public static void main9(String[] args) {
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        ArrayList<Double> arrayList2 = new ArrayList<>();
        List<? extends Number> list1 = arrayList1;
        //List<? extends Number> list2 = arrayList2;
        //list1.add(0,1);
        //list1.add(0,10.2);
        //通配符的上界 不适合写入数据
        Number o = list1.get(0);
        //Integer a = list1.get(1);
    }

    public static void main8(String[] args) {
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        Alg3.print1(list1);
        Alg3.print2(list1);
    }

    public static void main7(String[] args) {
        Alg<Integer> alg1 = new Alg<>();
        System.out.println(alg1);
        Alg<Integer> alg2 = new Alg<>();
        System.out.println(alg2);
    }

    public static void main6(String[] args) {
        Integer[] array = {1, 12, 4, 5};
        System.out.println(Alg2.findMaX(array));
    }

    public static void main5(String[] args) {
        Alg<Integer> alg1 = new Alg<>();
        Integer[] array = {1, 2, 3, 4};
        System.out.println(alg1.findMaX(array));

        Alg<String> alg2 = new Alg<>();
        String[] array2 = {"abc", "hello", "csde", "5452"};
        System.out.println(alg2.findMaX(array2));
    }

    public static void main3(String[] args) {
        //MyArray2<String> myArray = new MyArray2<String>();
        //String[] string = myArray.getArray();//编译器此时认为并不安全
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        Object[] ret = arrayList.toArray();
    }

    public static void main2(String[] args) {
        //MyArray2<String> myArray = new MyArray2<String>();
        MyArray2<Integer> myArray21 = new MyArray2<>();
        MyArray2<Number> myArray3 = new MyArray2<>();
        myArray3.set(1, 21);
        myArray3.set(1, 21.6);
        //yArray.set(0, "hello");
        //myArray.set(1, "10"); //1.编译的时候，自动进行类型的检查
        //String string = myArray.get(0);//2.不需要在进行强制类型转化，编译器会自动帮我们进行类型的转化

        MyArray2<Integer> myArray2 = new MyArray2<Integer>();
        //MyArray2<int> myArray3 = new MyArray2<int>(); 简单类型 基本类型 不能作为泛型类型的参数

        MyArray2 myArray1 = new MyArray2();
        // myArray1.set(0, "hello");
        myArray1.set(1, 10);
        //String string1 = (String) myArray.get(0);
    }

    public static void main1(String[] args) {
        MyArray1 myArray = new MyArray1();
        myArray.set(0, "hello");
        myArray.set(1, 10);
        String string = (String) myArray.get(0);
    }
}
